//
//  XYFunctionViewController.m
//  新项目
//
//  Created by xmy on 2017/2/24.
//  Copyright © 2017年 xmy. All rights reserved.
//

#import "XYFunctionViewController.h"
#import "SDCycleScrollView.h"
#import "ZFBBusinessTypeView.h"
#import "XYHeadLineView.h"
@interface XYFunctionViewController ()<SDCycleScrollViewDelegate>
/**
 *  原型图片数组
 */
@property(strong,nonatomic)NSMutableArray *cycleDataArray;
@property(strong,nonatomic) ZFBBusinessTypeView* businessTypeView;

//功能分类数组数据
@property(strong,nonatomic)NSArray *functionArray;
@end

@implementation XYFunctionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //解析数据
    [self model];
    
}
- (void)setupUI{
    
    //加载轮播器图片
    NSMutableArray *arr = [NSMutableArray new];
    for (int index = 0; index < 8; index++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d-%d",index,index]];
        [arr addObject:image];
    }
    self.cycleDataArray = arr;
    
    CGFloat w = self.view.bounds.size.width;
//    UIScrollView *demoContainerView = [[UIScrollView alloc] initWithFrame:self.view.frame];
//    demoContainerView.contentSize = CGSizeMake(self.view.frame.size.width, 70);
//    [self.view addSubview:demoContainerView];
//    //    [self.tableView.tableHeaderView addSubview: demoContainerView];
    
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, w, 180) shouldInfiniteLoop:YES imageNamesGroup:self.cycleDataArray];
    cycleScrollView.pageControlDotSize = CGSizeMake(5, 5);
    cycleScrollView.currentPageDotColor = [UIColor yellowColor];
    cycleScrollView.delegate = self;
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    [self.view addSubview:cycleScrollView];
//    [demoContainerView addSubview:cycleScrollView];
    cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    //功能按钮
    self.businessTypeView = [[ZFBBusinessTypeView alloc]init];
    
    [self.view addSubview:self.businessTypeView];
    
    [self.businessTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cycleScrollView.mas_bottom);
        
        make.left.right.equalTo(self.view);
        
        make.height.offset(180);
    }];
    
    __weak __typeof(self)wself = self;
    self.businessTypeView.btnTagBlock = ^(NSDictionary *dict){
        
        NSString *str = [dict objectForKey:@"btn.tag"];
        NSInteger tag= [str integerValue]; 
        [wself buttonDidChangeNotifacation:tag];
        
    };
    
    self.businessTypeView.buinessTypeData = self.functionArray;
    
    //文字轮播
    XYHeadLineView *chociseView = [[XYHeadLineView alloc]init];
//    chociseView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
    [self.view addSubview:chociseView];
    
    [chociseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.businessTypeView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.offset(40);
    }];
}

- (void)buttonDidChangeNotifacation:(NSInteger )tag{

    
    NSString *className = [NSString string];
    
    switch (tag) {
        case 0:
            NSLog(@"跳转到签到");
            className = @"DMWFuliViewController";
            break;
        case 1:
            NSLog(@"跳转到附近");
            className = @"DMTonariController";
            break;
        case 2:
            NSLog(@"跳转到日历");
            className = @"DMWCalenderTableViewController";
            break;
        case 3:
            NSLog(@"跳转到福利");
            className = @"DMFreeFuliViewController";
            break;
        case 4:
            NSLog(@"跳转到玩乐");
            className = @"DMHaveFunController";
            break;
        case 5:
            NSLog(@"跳转到电影");
            className = @"DMWFilmViewController";
            break;
        case 6:
            NSLog(@"跳转到体育");
            className = @"DMSportViewController";
            break;
        case 7:
            NSLog(@"跳转到亲子");
            className = @"DMParentChildViewController";
            break;
            
        default:
            break;
    }
    
    
//    [self.navigationController pushViewController:[NSClassFromString(className) new] animated:YES];
    //    DMWNavigationController *nav = [[DMWNavigationController alloc]initWithRootViewController:[NSClassFromString(className) new]];
    //
    //    [self.navigationController pushViewController:nav animated:YES];
    
}
//解析数据
- (void)model{
    
    
    NSURL *URL = [NSURL URLWithString:@"http://mapi.damai.cn/Proj/Panev3.aspx?cityid=852"];
    
    // 2.session
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 5.处理响应
        if (error==nil && data!=nil && data.length>0) {
            
            // 反序列化
            NSDictionary * result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
            
            NSArray *arr = result[@"btnCtx"];
            
            self.functionArray = [NSArray yy_modelArrayWithClass:NSClassFromString(@"DMFunctionModel") json:arr];
            
        } else{
            NSLog(@"出错 %@",error);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self setupUI];
        });
        
    }];
    // 4.启动任务(resume)
    [dataTask resume];//这句话一定不要忘记
    
}

#pragma mark - SDCycleScrollViewDelegate图片点击跳转页面

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    
    //    [self.navigationController pushViewController:[NSClassFromString(@"DemoVCWithXib") new] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
